package gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.IOException;
import java.net.UnknownHostException;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;

import controller.DatabaseController;

/**
 * @author Stanley
 * 
 */
public class Login extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private final JTextField txtServerIP;
	private final JTextField txtPassword;
	private final JTextField txtUsername;
	private final JTextField txtPort;
	private boolean isLogin = false;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			Login dialog = new Login();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public Login() {
		setModalityType(ModalityType.APPLICATION_MODAL);
		setResizable(false);
		setTitle("DBMS Login");
		setBounds(100, 100, 329, 207);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblServerName = new JLabel("Server name : ");
			lblServerName.setBounds(5, 5, 97, 20);
			lblServerName.setHorizontalAlignment(SwingConstants.RIGHT);
			contentPanel.add(lblServerName);
		}
		{
			txtServerIP = new JTextField();
			txtServerIP.setBounds(112, 5, 195, 20);
			txtServerIP.setText("127.0.0.1");
			contentPanel.add(txtServerIP);
			txtServerIP.setColumns(10);
		}
		{
			JLabel lblUsername = new JLabel("Username : ");
			lblUsername.setBounds(5, 76, 97, 20);
			lblUsername.setHorizontalAlignment(SwingConstants.RIGHT);
			contentPanel.add(lblUsername);
		}
		{
			txtUsername = new JTextField();
			txtUsername.addFocusListener(new FocusAdapter() {
				@Override
				public void focusGained(FocusEvent arg0) {
					txtUsername.setText("");
				}
			});
			txtUsername.setBounds(112, 76, 195, 20);
			txtUsername.setText("username");
			contentPanel.add(txtUsername);
			txtUsername.setColumns(10);
		}
		{
			JLabel lblPassword = new JLabel("Password : ");
			lblPassword.setBounds(5, 104, 97, 27);
			lblPassword.setHorizontalAlignment(SwingConstants.RIGHT);
			lblPassword.setFont(new Font("Tahoma", Font.PLAIN, 11));
			contentPanel.add(lblPassword);
		}
		{
			txtPassword = new JPasswordField();
			txtPassword.addFocusListener(new FocusAdapter() {
				@Override
				public void focusGained(FocusEvent arg0) {
					txtPassword.setText("");
				}
			});
			txtPassword.setBounds(112, 107, 195, 20);
			txtPassword.setText("password");
			contentPanel.add(txtPassword);
			txtPassword.setColumns(10);
		}

		JLabel lblPort = new JLabel("Port : ");
		lblPort.setHorizontalAlignment(SwingConstants.RIGHT);
		lblPort.setBounds(5, 36, 97, 14);
		contentPanel.add(lblPort);

		txtPort = new JTextField();
		txtPort.setBounds(112, 36, 195, 20);
		contentPanel.add(txtPort);
		txtPort.setColumns(10);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton btnOK = new JButton("OK");
				btnOK.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent arg0) {
						try {
							DatabaseController dbCtl = new DatabaseController(
									"");
							String ip = txtServerIP.getText();
							int port = Integer.parseInt(txtPort.getText());
							String username = txtUsername.getText();
							String password = txtPassword.getText();
							dbCtl.connectToServer(ip, port);
							try {
								boolean loginRes = dbCtl.login(username,
										password);
								if (loginRes) {
									JOptionPane.showMessageDialog(contentPanel,
											"Login Successfully");
									isLogin = true;
									setVisible(false);
									return;
								} else {
									JOptionPane.showMessageDialog(contentPanel,
											"Login Failed");
									return;
								}
							} catch (Exception e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
								JOptionPane.showMessageDialog(contentPanel,
										e.getMessage());
							}
						} catch (NumberFormatException e) {
							e.printStackTrace();
							JOptionPane.showMessageDialog(contentPanel,
									e.getMessage());
							return;
						} catch (UnknownHostException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							JOptionPane.showMessageDialog(contentPanel,
									e.getMessage());
							return;
						} catch (IOException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
							JOptionPane.showMessageDialog(contentPanel,
									e.getMessage());
							return;
						}
					}
				});
				btnOK.setActionCommand("OK");
				buttonPane.add(btnOK);
				getRootPane().setDefaultButton(btnOK);
			}
			{
				JButton btnCancel = new JButton("Cancel");
				btnCancel.addMouseListener(new MouseAdapter() {
					@Override
					public void mouseClicked(MouseEvent e) {
						System.exit(0);
					}
				});
				btnCancel.setActionCommand("Cancel");
				buttonPane.add(btnCancel);
			}
		}
	}

	public boolean getIsLogin() {
		return this.isLogin;
	}
}
